package aynu.hd.Hotel.Service.Impl;

import aynu.hd.Hotel.Dao.orderDao;
import aynu.hd.Hotel.Dao.roomDao;
import aynu.hd.Hotel.Dao.scheduleDao;
import aynu.hd.Hotel.Service.orderService;
import aynu.hd.pojo.Orders;
import aynu.hd.pojo.Room;
import aynu.hd.pojo.Schedule;
import aynu.hd.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
public class orderServiceImpl implements orderService {
    @Autowired
    orderDao orderDao;
    @Autowired
    scheduleDao scheduleDao;
    @Autowired
    roomDao roomDao;

    //付款生成订单
    @Transactional
    @Override
    public String addOrder(Orders orders, String num, String room_id, String schedule_id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        orders.setUser(user);
        if(orders.getOrders_name()==null){
            orders.setOrders_name(user.getUser_truename());
            orders.setOrders_sex(user.getUser_sex());
            orders.setOrders_identity_card(user.getUser_identity_card());
        }
        Integer i = orderDao.addOrders(orders);
        if (i == 0) {
            return "FAIL";
        }
        //更新预定房间信息的预定数量，并且和订单关联
        scheduleDao.updateScheduleDaoRoomCount(num, schedule_id, orders.getOrders_id());
        new Thread(()->{
            try{
                Thread.sleep(1000*60);//测试1分钟
                //Thread.sleep(1000*60*60);//一小时
                Orders orders1 = orderDao.queryOrderById(orders.getOrders_id());
                if(orders1!=null){
                    if (orders1.getOrders_state() == 0) {
                        orderDao.deleteOrderById(orders1.getOrders_id());
                        scheduleDao.removeScheduleByOrderId(orders1.getOrders_id());
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }).start();
        session.setAttribute("orders",orders);
        return "OK";
    }

    //支付成功操作
    @Transactional
    @Override
    public void paySuccess(String order_id, HttpSession session) {
        //之前在scheduleDao里面映射需要，就直接调用了，根据id查找订单
        orderDao.updateStateById(order_id,"1");
        Schedule schedule = scheduleDao.queryScheduleByOrderId(Integer.valueOf(order_id));
        //房间数量减少
        roomDao.addRoomLive(schedule.getRoom().getRoom_id(),schedule.getSchedule_amount()+schedule.getRoom().getRoom_live());
        int a = schedule.getRoom().getRoom_amount()-schedule.getSchedule_amount();
        roomDao.updateRoomAmount(schedule.getRoom().getRoom_id().toString(), a);
        session.removeAttribute("orders");
        new Thread(()->{
            try{
                int day = schedule.getOrders().getOrders_date();
                Thread.sleep(1000*60);//测试、1分钟
                //Thread.sleep(1000*60*60*24*day);//一天*天数
                Room room = roomDao.queryRoomById(String.valueOf(schedule.getRoom().getRoom_id()));
                int b = room.getRoom_amount() + schedule.getSchedule_amount();
                roomDao.updateRoomAmount(room.getRoom_id().toString(), b);
            }catch(Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    //未支付订单信息
    @Transactional
    @Override
    public List<Schedule> queryOrderByUser(HttpSession session) {
        User user = (User) session.getAttribute("user");
        //预定的信息是已支付
        List<Schedule> schedules = scheduleDao.queryScheduleByUserId(user.getUser_id(),"已支付");
        List<Schedule> scheduleList = new ArrayList<>();
        //订单0代表未支付的订单
        List<Orders> orders = orderDao.queryOrderByUserId(user.getUser_id(),"0");
        for (Orders order : orders) {
            for (Schedule schedule : schedules) {
                if(schedule.getOrders().getOrders_id() == order.getOrders_id()){
                    scheduleList.add(schedule);
                    break;
                }
            }
        }
        return scheduleList;
    }

    //已支付订单信息
    @Transactional
    @Override
    public List<Schedule> queryOrderByUser1(HttpSession session) {
        User user = (User) session.getAttribute("user");
        List<Schedule> schedules = scheduleDao.queryScheduleByUserId(user.getUser_id(),"已支付");
        List<Schedule> scheduleList1 = new ArrayList<>();
        List<Orders> orders = orderDao.queryOrderByUserId(user.getUser_id(),"1");
        for (Orders order : orders) {
            for (Schedule schedule : schedules) {
                if(schedule.getOrders().getOrders_id() == order.getOrders_id()){
                    scheduleList1.add(schedule);
                    break;
                }
            }
        }
        return scheduleList1;
    }
}
